#include "BlockQueue.hpp"
#include <pthread.h>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>

void *consumer(void *args)
{
    BlockQueue<int>* bq = static_cast<BlockQueue<int> *>(args);
    while(true)
    {
        //1.消费数据
        int data = 0;
        bq->Pop(&data);
        //2.对数据进行处理
        cout << "consumer data: " << data << endl;
    }
    return nullptr;
}

void *productor(void *args)
{
    BlockQueue<int>* bq = static_cast<BlockQueue<int> *>(args);
    while(true)
    {
        //1.有数据
        int data = rand() % 10 + 1; //[1, 10]
        //2.进行生产
        bq->Push(data);
        cout << "productor data: " << data << endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    srand((uint16_t)time(nullptr) ^ getpid() ^ pthread_self()); //形成更加随机的数
    BlockQueue<int> *bq = new BlockQueue<int>();
    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, bq);
    pthread_create(&c, nullptr, productor, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    return 0;
}